package mns.modules.output.sms;

import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;

import mns.*;
import mns.modules.output.email.EMail;

/**
 * A generic Text Message Module
 * Provides messaging to mobile phones via. cell numbers and carrier email extensions:
 * 
 * Carrier Email Address List (12)
 *  1. AT&T: number@txt.att.net
 *  2. Qwest: number@qwestmp.com
 *  3. T-Mobile: number@tmomail.net
 *  4. Verizon: number@vtext.com
 *  5. Sprint: number@messaging.sprintpcs.com or <b>number@pm.sprint.com<b>
 *  6. Virgin Mobile: number@vmobl.com
 *  7. Nextel: number@messaging.nextel.com
 *  8. Alltel: number@message.alltel.com
 *  9. Metro PCS: number@mymetropcs.com
 *  10. Powertel: number@ptel.com
 *  11. Suncom: number@tms.suncom.com
 *  12. U.S. Cellular: number@email.uscc.net
 * 
 * @author Dan Moore
 *
 */
public class SMSModule implements MNSOutputModule {
	
	private static enum Carriers {
		ATT, QWEST, TMOBILE, VERIZON, SPRINT, VIRGIN,
		NEXTEL, ALLTEL, METROPCS, POWERTEL, SUNCOM, USC 
	}
	
	private String phoneToEmail(String phone, Carriers cr) {
		String email = null;
		
		switch(cr) {
			case ATT:
				email = phone+="@txt.att.net";
				break;
			case QWEST:
				email = phone+="@qwestmp.com";
				break;
			case TMOBILE:
				email = phone+="@tmomail.net";
				break;
			case VERIZON:
				email = phone+="@vtext.com";
				break;
			case SPRINT:
				email = phone+="@pm.sprint.com";
				break;
			case VIRGIN:
				email = phone+="@vmobl.com";
				break;
			case NEXTEL:
				email = phone+="@messaging.nextel.com";
				break;
			case ALLTEL:
				email = phone+="@message.alltel.com";
				break;
			case METROPCS:
				email = phone+="@mymetropcs.com";
				break;
			case POWERTEL:
				email = phone+="@ptel.com";
				break;
			case SUNCOM:
				email = phone+="@tms.suncom.com";
				break;
			case USC:
				email = phone+="@email.uscc.net";
				break;
			default:
				return null;
		}
		return email;
	}
	
	public boolean sendMessage(MNSMessage msg, UserOptions opts) {
		UserOption opt = null;
		String email = null;
		String phone = null;
		Carriers cr;
		
		try {
			opt = opts.getOption("phone");
			phone = opt.getValue();
			opt = opts.getOption("carrier");
			cr = Carriers.valueOf(opt.getValue());
			if((email = phoneToEmail(phone,cr)) == null)
				throw new NullPointerException();
		} catch(NullPointerException e) {
			System.err.println("Bad User Options, not sending SMS.");
			return false;
		}

		try {
			EMail.sendMail(email, "msg@mns.com", "", msg.getShortMessage());
		} catch (NoSuchProviderException e) {
			System.err.println(e.toString() + "in SMSModule.sendMessage().");
			return false;
		} catch (MessagingException e) {
			System.err.println(e.toString() + "in SMSModule.sendMessage().");
			return false;
		}
		return true;
	}

	public String getName() {
		return "Limited SMS by E-Mail Output Module";
	}

	public String getDescriptionHTML() {
		return "Don't see your carrier? Program a new output module today!<br>Contact: 404-939-4326!";
	}

	public String getOptionsFormHTML() {
		String form = new String();
		
		form+="Phone Number: <input type=\"text\" name=\"phone\" value=\"1234567890\" /><br>";
		
		form+="Carrier: <select name=\"carrier\">";
		form+="<optgroup label=\"Carriers\">";
		form+="<option value=\""+Carriers.ATT+"\">AT&T</option>";
		form+="<option value=\""+Carriers.QWEST+"\">Qwest</option>";
		form+="<option value=\""+Carriers.TMOBILE+"\">T-Mobile</option>";
		form+="<option value=\""+Carriers.VERIZON+"\">Verizon</option>";
		form+="<option value=\""+Carriers.SPRINT+"\">Sprint</option>";
		form+="<option value=\""+Carriers.VIRGIN+"\">Virgin Mobile</option>";
		form+="<option value=\""+Carriers.NEXTEL+"\">Nextel</option>";
		form+="<option value=\""+Carriers.ALLTEL+"\">Alltel</option>";
		form+="<option value=\""+Carriers.METROPCS+"\">Metro PCS</option>";
		form+="<option value=\""+Carriers.POWERTEL+"\">Powertel</option>";
		form+="<option value=\""+Carriers.SUNCOM+"\">Suncom</option>";
		form+="<option value=\""+Carriers.USC+"\">U.S. Cellular</option>";
		form+="</optgroup></select>";
		
		return form;
	}

}
